package edu.hawaii.senin.iclouds.view;

import javax.swing.table.AbstractTableModel;

import edu.hawaii.senin.iclouds.basics.PopulationSimple;
import edu.hawaii.senin.iclouds.basics.Specie;

/**
 * Implements table container for the population.
 * 
 * @author Pavel Senin.
 * 
 */
public class PopulationTableModel
    extends AbstractTableModel {

  /**
   * Eclipse generated serial num.
   */
  private static final long serialVersionUID = 4428161400982769301L;

  private static final String[] ColumnNames = { "Specie", "Name", "AMU", "Abundance", "Fraction",
      "Accretion Probability", "Type", "Velocity", "Accumulator" };

  private Object[][] rowData;

  /**
   * Generic constructor.
   * 
   */
  public PopulationTableModel() {
    super();
    this.rowData = new Object[1][9];
  }

  /**
   * Reports column count.
   * 
   * @return column count.
   */
  public int getColumnCount() {
    return PopulationTableModel.ColumnNames.length;
  }

  /**
   * Reports column names.
   * 
   * @param col column specified.
   * @return specified column name.
   */
  @Override
  public String getColumnName(int col) {
    return PopulationTableModel.ColumnNames[col];
  }

  /**
   * Reports table value.
   * 
   * @param rowIdx row specified.
   * @param colIdx column specified.
   * @return table value.
   */
  public Object getValueAt(int rowIdx, int colIdx) {
    return rowData[rowIdx][colIdx];
  }

  /**
   * Reports row count.
   * 
   * @return row count.
   */
  public int getRowCount() {
    return rowData.length;
  }

  /**
   * Reports whether or not cspecified cell is editable.
   * 
   * @param rowIdx row specified.
   * @param colIdx column specified.
   * @return true if editable.
   */
  @Override
  public boolean isCellEditable(int rowIdx, int colIdx) {
    return false;
  }

  /**
   * Sets the cell value.
   * 
   * @param value value to set.
   * @param rowIdx row specified.
   * @param colIdx column specified.
   */
  @Override
  public void setValueAt(Object value, int rowIdx, int colIdx) {
    rowData[rowIdx][colIdx] = value;
    fireTableCellUpdated(rowIdx, colIdx);
  }

  /**
   * Sets whole table with data provided.
   * 
   * @param p population collection.
   */
  public void setPopulationData(PopulationSimple p) {
    Integer rows = p.getPopulationSpeciesNum();
    Integer columns = ColumnNames.length;
    rowData = new Object[rows][columns];
    Integer rowIdx = 0;
    for (Specie s : p) {
      Object[] arr = s.toArray();
      for (int i = 0; i < arr.length; i++) {
        rowData[rowIdx][i] = arr[i];
      }
      rowIdx++;
    }
    fireTableDataChanged();
  }

}
